#include "uv_timer.h"


uv_timer::uv_timer()
{
}


uv_timer::~uv_timer()
{
	stop_timer();
}

void uv_timer::start_timer(uint64_t timeout, uint64_t repeat, timer_cb cbTimer, void* userData)
{	
	int nRet = 0;	
	nRet = uv_timer_init(g_loop, &m_uvTimer);
	if (nRet != 0)
	{
		printf("AddTimer uv_timer_init failed[%d][%s]", nRet, uv_strerror(nRet));		
		return;
	}
	if (repeat){
		m_brepeat=true;
	}
	m_timercb = cbTimer;
	m_usrData = userData;
	m_uvTimer.data=this;
	nRet = uv_timer_start(&m_uvTimer, uv_timer::on_timer, timeout, repeat);
	m_bRumTimer=true;
}


void uv_timer::stop_timer()
{
	int nRet = 0;	
	if (!m_bRumTimer)return;
	nRet = uv_timer_stop(&m_uvTimer);				
	m_bRumTimer=false;	
}

void uv_timer::on_timer(uv_timer_t* rTimer)
{
	if ((NULL == rTimer) || (NULL == rTimer->data))
	{	
		return;
	}
	uv_timer* timerPtr = (uv_timer*)rTimer->data;
	if (timerPtr->m_timercb!= NULL)
	{
		timerPtr->m_timercb(timerPtr->m_usrData);
	}
}
